<template>
  <div class="user-container">
    <!-- 已登录 -->
    <!-- 头部 -->
    <div class="info-wrap" v-if="user">
      <van-cell 
        class="base-info"
        title="单元格" 
        value="内容" 
        center
        :border="false"
      >
        <van-image
          class="user-photo"
          slot="icon"
          fit="cover"
          round
          :src="userInfo.photo"
        />
        <div class="user-nickname" slot="title">{{userInfo.name}}</div>
        <van-button 
          class="update-btn" 
          type="default" 
          round
          to="/user/profile"
        >
          编辑资料
        </van-button>
      </van-cell>
      <!-- 四个宫格 -->
      <van-grid 
        class="data-info"
        :border="false"
      >
        <van-grid-item class="data-info-item">
          <div class="text-wrap" slot="text">
            <div class="number">{{userInfo.art_count}}</div>
            <div class="title">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div class="text-wrap" slot="text">
            <div class="number">{{userInfo.follow_count}}</div>
            <div class="title">关注</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item"> 
          <div class="text-wrap" slot="text">
            <div class="number">{{userInfo.fans_count}}</div>
            <div class="title">粉丝</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div class="text-wrap" slot="text">
            <div class="number">{{userInfo.like_count <=0 ? 0 : userInfo.like_count}}</div>
            <div class="title">获赞</div>
          </div>
        </van-grid-item>
      </van-grid>
    </div>

    <!-- 未登录 -->
    <div v-else class="not-login">
      <div @click="toLogin">
        <img class="mobile" src="../../assets/mobile.png" alt="">
      </div>
      <div class="text">登录/注册</div>
    </div>

    <!-- 历史 & 收藏 -->
    <van-grid 
      class="nav-grid mb-4"
      :column-num="2"
    >
        <van-grid-item class="nav-grid-item" icon-prefix="toutiao" icon="shoucang" text="收藏" />
        <van-grid-item class="nav-grid-item" icon-prefix="toutiao" icon="lishi" text="历史" />
    </van-grid>
    <!-- 消息通知 & 小智同学 -->
    <van-cell title="消息通知" is-link to="/" />
    <van-cell class="mb-4" title="小智同学" is-link to="/user/chat" />
    <!-- 退出登录 -->
    <van-cell 
      v-if="user"
      class="login-out"
      title="退出登录" 
      @click="loginOut"
    />
  </div>
</template>

<script>
import { mapState } from "vuex";
import {userInfoApi} from '@/api/user'

export default {
  name: "User",
  components: {},
  props: {},
  data() {
    return {
      // 这个名字不能和mapState中的user重名 
      userInfo: {}
    };
  },
  watch: {},
  created() {
    // 获取用户信息请求
    this.getUserInfoApi()
  },
  computed: {
    ...mapState(["user"])
  },
  activated () {
    if (this.$store.state.user) {
      this.getUserInfoApi()
    }
  },
  mounted() {
  },
  methods: {
    // 去登陆
    toLogin() {
      this.$router.push('/login')
      // this.$router.push({
      //   name: 'Login',
      //   query: {
      //     redirect: '/user'
      //   }
      // })
    },
    // 退出登录
    loginOut() {
      this.$dialog.confirm({
        title: '提示',
        message: '确认退出吗',
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser',null);
        })
        .catch(() => {
          // on cancel
        });
    },
    // 获取用户信息请求
    async getUserInfoApi() {
      const {data} = await userInfoApi();
      this.userInfo = data.data;
    }
  
  },
};

/**
 * 1、组件的样式作用域，默认是全局样式。
 *   + 如果不希望当前组件中的样式影响别的组件，我们可以添加作用域。<scoped>
 * 
 * 2、自己给设置的class,会默认作用到组件的根节点。
 * 
 * 3、在有作用域的组件中如何给子组件设置样式？
 *   + 默认只能作用到子组件的根节点
 *      + 使用子组件根节点本身的class类名
 *      + 如果是第三方组件你不知道它的根节点的类名，那就审查元素查看，或者添加一个类名
 *      + 组件的class，会默认作用到组件的根节点。
 *      + 建议使用我们自己的类名
 *   
 *  /deep/：深度作用操作符  也可以写成 ”>>>“、”::v-deep“
 * 
*/
</script>

<style lang='less' scoped>
//@import url(); 引入公共css类
.user-container {
  .info-wrap {
    background: url("../../assets/banner.png") no-repeat;
    background-size: cover;
    .base-info {
      box-sizing: border-box;
      height: 115px;
      padding-top: 38px;
      padding-bottom: 11px;
      background-color: unset;
      .user-photo {
        box-sizing: border-box;
        width: 66px;
        height: 66px;
        border: 1px solid #fff;
        margin-right: 11px;
      }
      .user-nickname {
        color: #fff;
        font-size: 15px;
      }
      .update-btn {
        height: 20px;
        color: #666;
        font-size: 10px;
      }
    }
    /deep/ .van-grid-item__content {
      background: unset;
    }
    .data-info {
      .data-info-item {
        height: 65px;
        color: #fff;
        .text-wrap {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .number {
            margin-bottom: 4px;
            font-size: 18px;
          }
          .title {
            font-size: 11px;
          }
        }
      }   
    }
    
  }
  .not-login {
    height: 180px;
    background: url("../../assets/banner.png") no-repeat;
    background-size: cover;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile {
      width: 66px;
      height: 66px;
      margin-bottom: 4px;
    }
    .text {
      font-size: 14px;
      color: #fff;
    }
  }

  /deep/ .nav-grid {
    .nav-grid-item {
      height: 70px;
      .toutiao {
        font-size: 22px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .van-grid-text {
        color: #333;
        font-size: 14px;
      }
    }
  }
  .login-out {
    text-align: center;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 4px;
  }
}

</style>